<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理员登录 - 电子礼金系统</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <style>
        .gradient-bg {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
    </style>
</head>
<body class="gradient-bg min-h-screen flex items-center justify-center p-4">
    <div class="bg-white rounded-2xl p-8 w-full max-w-md shadow-2xl">
        <div class="text-center mb-8">
            <h1 class="text-3xl font-bold text-gray-800 mb-2">管理员登录</h1>
            <p class="text-gray-600">电子礼金SaaS系统</p>
        </div>
        
        <form id="admin-login-form" class="space-y-6">
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">用户名</label>
                <input type="text" id="username" class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="请输入管理员用户名" required>
            </div>
            
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">密码</label>
                <input type="password" id="password" class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="请输入密码" required>
            </div>
            
            <button type="submit" class="w-full bg-blue-600 text-white py-3 rounded-lg hover:bg-blue-700 transition duration-200 font-medium">
                登录
            </button>
        </form>
        
        <div class="mt-6 text-center">
            <a href="/" class="text-blue-600 hover:text-blue-800 text-sm">
                ← 返回H5应用
            </a>
        </div>
        
        <div class="mt-8 p-4 bg-yellow-50 rounded-lg">
            <h3 class="text-sm font-medium text-yellow-800 mb-2">默认管理员账号</h3>
            <p class="text-xs text-yellow-700">用户名: admin</p>
            <p class="text-xs text-yellow-700">密码: admin123</p>
        </div>
    </div>

    <script>
        // 工具函数
        function showMessage(message, type = 'info') {
            const colors = {
                success: 'bg-green-500',
                error: 'bg-red-500',
                info: 'bg-blue-500'
            };
            
            const messageEl = document.createElement('div');
            messageEl.className = `fixed top-4 right-4 ${colors[type]} text-white px-6 py-3 rounded-lg shadow-lg z-50`;
            messageEl.textContent = message;
            document.body.appendChild(messageEl);
            
            setTimeout(() => {
                messageEl.remove();
            }, 3000);
        }

        // API调用函数
        async function apiCall(endpoint, options = {}) {
            const config = {
                headers: {
                    'Content-Type': 'application/json'
                },
                ...options
            };

            try {
                const response = await fetch('/api' + endpoint, config);
                const data = await response.json();
                
                if (!response.ok) {
                    throw new Error(data.error || '请求失败');
                }
                
                return data;
            } catch (error) {
                console.error('API调用失败:', error);
                throw error;
            }
        }

        // 管理员登录
        async function adminLogin(username, password) {
            try {
                // 这里应该调用管理员登录API
                // 暂时使用模拟登录
                if (username === 'admin' && password === 'admin123') {
                    localStorage.setItem('admin_token', 'admin-token-' + Date.now());
                    localStorage.setItem('admin_user', JSON.stringify({
                        id: 'admin',
                        username: 'admin',
                        role: 'admin'
                    }));
                    
                    showMessage('登录成功', 'success');
                    setTimeout(() => {
                        window.location.href = '/admin.html';
                    }, 1000);
                } else {
                    showMessage('用户名或密码错误', 'error');
                }
            } catch (error) {
                showMessage(error.message, 'error');
            }
        }

        // 事件监听器
        document.addEventListener('DOMContentLoaded', function() {
            // 检查是否已登录
            const token = localStorage.getItem('admin_token');
            if (token) {
                window.location.href = '/admin.html';
                return;
            }

            // 登录表单
            document.getElementById('admin-login-form').addEventListener('submit', function(e) {
                e.preventDefault();
                const username = document.getElementById('username').value;
                const password = document.getElementById('password').value;
                adminLogin(username, password);
            });
        });
    </script>
</body>
</html>
